package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import com.supply.dl.R;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.SearchStockBean;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.bean.WorkNotesBean;
import com.supply.dl.bean.request.InStorageRequest;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.StringUtils;

import java.util.ArrayList;

/**
 * 在库整理
 */
public class ReorganizeActivity extends BaseActivity
{
	private ArrayList<WorkNotesBean> remarks = new ArrayList<>();

	@BindView(R.id.txt_select_warehouse) TextView txtSelectHouse;

	@BindView(R.id.txt_house_lbs) TextView txtHoustLbs;

	@BindView(R.id.txt_work_record) TextView txtWorkRecord;

	@BindView(R.id.edit_work_name) EditText editWorkName;
	@BindView(R.id.txt_save) TextView txtSave;
	@BindView(R.id.edit_why) EditText editWhy;

	@Override protected int getLayoutResource()
	{
		return R.layout.activity_reorganize;
	}

	@Override protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTitleShow(getResourcesString(R.string.in_storage_consolidation));
		showLeftBack();
		txtHoustLbs.setOnClickListener(this);
		txtSelectHouse.setOnClickListener(this);
		txtWorkRecord.setOnClickListener(this);
		txtSave.setOnClickListener(this);
	}

	private SelectDictionaryBean afterBeanCu;
	private SelectDictionaryBean searchStockBean;
	public static final int AFTER_CK_LBS = 102;

	@Override public void onClick(View v)
	{
		super.onClick(v);
		Intent intent = new Intent(this, SelectBaseDataActivity.class);
		switch (v.getId())
		{
		case R.id.txt_select_warehouse:
			intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_WAREHOUSE);
			startActivityForResult(intent, AppConfig.BaseDataAction.REQCODE_REORIGE_CODE);
			break;

		case R.id.txt_house_lbs:
			if (afterBeanCu == null)
			{
				return;
			}
			//Intent intents = new Intent(this, DepotSelectActivity.class);
			intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_STOCK_POSITION);
			intent.putExtra("whId", afterBeanCu.getValue());
			startActivityForResult(intent, AFTER_CK_LBS);
			break;

		case R.id.txt_save:
			inStorage();
			break;

		case R.id.txt_work_record:   //选择作业记录
			Intent intent1 = new Intent(this, OperateRemarkActivity.class);
			startActivityForResult(intent1, 105);
			break;
		}
	}

	@Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null && requestCode == 105)
		{
			remarks = (ArrayList<WorkNotesBean>) data.getSerializableExtra("remarks");
			if (remarks.size() > 0)
			{
				String workNoteName = "";
				for (int i = 0; i < remarks.size(); i++)
				{
					if (StringUtils.isBlank(workNoteName))
						workNoteName = remarks.get(i).getDepart();
					else
						workNoteName = workNoteName + "," + remarks.get(i).getDepart();
				}
				txtWorkRecord.setText(workNoteName + "");
			}
			return;
		}
		if (data == null || data.getSerializableExtra("selectBean") == null)
			return;
		switch (requestCode)
		{
		case AppConfig.BaseDataAction.REQCODE_REORIGE_CODE:
			afterBeanCu = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
			txtSelectHouse.setText(afterBeanCu.getText());
			break;
		case AFTER_CK_LBS:
			searchStockBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
			txtHoustLbs.setText(searchStockBean.getText());
			break;
		}
	}

	/**
	 * 保存在库整理信息
	 */
	private void inStorage()
	{
		if (afterBeanCu == null || searchStockBean == null) {
			showToast("请完成相关信息");
			return;
		}
		HttpManger httpManger = HttpManger.getInstance();
		httpManger.setContext(this);
		InStorageRequest inStorageRequest = new InStorageRequest();
		inStorageRequest.setJobName(editWorkName.getText().toString().trim());
		inStorageRequest.setWhId(afterBeanCu.getValue());
		inStorageRequest.setStockId(searchStockBean.getValue());
		inStorageRequest.setJobReason(editWhy.getText().toString().trim());
		inStorageRequest.setJobAmountList(remarks);
		httpManger.inStorageInfo(inStorageRequest);
		httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
		{
			@Override public void onSuccess(BaseInfo<?> baseInfo)
			{
				showToast("保存在库整理作业成功");
				finish();
			}

			@Override public void onFailure(String message)
			{
				showToast(message);
			}
		});
	}
}
